/**
 * 
 */
package com.meetingshow.meeting.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.meetingshow.common.base.dao.BaseDAO;
import com.meetingshow.common.base.dao.model.QueryStruct;
import com.meetingshow.common.base.service.model.QueryResults;
import com.meetingshow.meeting.dao.model.MeetingPO;
import com.meetingshow.meeting.service.util.MeetingServiceParas;

/**
 * @author louyi
 *
 */
public class MeetingDAO extends BaseDAO {
    public Integer saveMeeting(MeetingPO meetingPO) throws Exception {
        return (Integer) this.saveBaseObj(meetingPO);
    }

    public void updateMeeting(MeetingPO meetingPO) throws Exception {
        this.updateBaseObj(meetingPO);
    }

    public void removeMeeting(MeetingPO meetingPO) throws Exception {
        this.deleteBaseObj(meetingPO);
    }
    
    public MeetingPO loadMeetingById(Integer meetingId) throws Exception {
        return this.queryById(MeetingPO.class, meetingId);
    }

    public QueryResults<MeetingPO> queryMeeting(QueryStruct queryStruct) throws Exception {
        QueryResults<MeetingPO> queryResults = new QueryResults<MeetingPO>();
        List<MeetingPO> resultList = findByParameter(queryStruct);
        queryResults.setResultList(resultList);
        if (queryStruct.getFirstResultNo() != null
                && queryStruct.getResultsPerGroup() != null) {
            queryResults = queryPageInfo(queryStruct, queryResults);
        } else {
            queryResults.setTotalResultsNum(Long.valueOf(resultList.size()));
        }
        return queryResults;
    }
    
    /**
     * Increase the following count of meeting by one.
     * @param meetingId
     * @throws Exception
     */
    public int addFollowCount(Integer meetingId) throws Exception {
        return updateFollowCount(meetingId, "+");
    }
    
    /**
     * Decrease the following count of meeting by one.
     * @param meetingId
     * @throws Exception
     */
    public int subFollowCount(Integer meetingId) throws Exception {
        return updateFollowCount(meetingId, "-");
    }
    
    /**
     * Update the following count of meeting by one.
     * @param meetingId
     * @param sign
     * @throws Exception
     */
    public int updateFollowCount(Integer meetingId, String sign) throws Exception {
        final String hql = "update " + MeetingServiceParas.MEETING_PO + " " 
                + MeetingServiceParas.MEETING_PO.toLowerCase()
                + " set " + MeetingServiceParas.MEETING_FOLLOWCOUNT 
                + "=" + MeetingServiceParas.MEETING_FOLLOWCOUNT + sign + "1"
                + " where " + MeetingServiceParas.MEETING_ID + "=:meetingId";
        
        final Integer queryMeetingId = meetingId;
        
        getHibernateTemplate().execute(new HibernateCallback() {
            @Override
            public Object doInHibernate(Session session) throws HibernateException,
                    SQLException {
                session.createQuery(hql).setInteger("meetingId", queryMeetingId).executeUpdate();
                return null;
            }
        });
        
        final String queryHql = "select " + MeetingServiceParas.MEETING_FOLLOWCOUNT + " "
                + "from " + MeetingServiceParas.MEETING_PO + " " 
                + MeetingServiceParas.MEETING_PO.toLowerCase()
                + " where " + MeetingServiceParas.MEETING_ID + "=:meetingId";
        
        return ((Integer) getHibernateTemplate().execute(new HibernateCallback() {
            @Override
            public Object doInHibernate(Session session) throws HibernateException,
                    SQLException {
                Object result = session.createQuery(queryHql).setInteger("meetingId", queryMeetingId).uniqueResult();
                return result;
            }
        })).intValue();
    }
    
    /**
     * Get the following count of meeting.
     * @param meetingId
     * @return
     * @throws Exception
     */
    public int getFollowCount(Integer meetingId) throws Exception {
        final String hql = "select " + MeetingServiceParas.MEETING_FOLLOWCOUNT + " from "
                + MeetingServiceParas.MEETING_PO + " " 
                + MeetingServiceParas.MEETING_PO.toLowerCase()
                + " where " + MeetingServiceParas.MEETING_ID + "=:meetingId";
        
        final Integer queryMeetingId = meetingId;
        
        Integer count = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
            @Override
            public Object doInHibernate(Session session) throws HibernateException,
                    SQLException {
                return session.createQuery(hql).setInteger("meetingId", queryMeetingId).uniqueResult();
            }
        });
        
        return count.intValue();
    }
}
